package com.eqdd.yiqidian.ui;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import com.eqdd.yiqidian.InputDialogActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.utils.ClickUtil;
import com.jakewharton.rxbinding.widget.RxTextView;

/**
 * @author吕志豪 .
 * @date 17-12-18  上午8:42.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class InputDialogActivity extends BaseActivity {

    private InputDialogActivityCustom dataBinding;

    @Override
    public void initBinding() {

        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_dialog_input);
        setWindow();
        showSoftInputFromWindow(this, dataBinding.etComment);
    }

    @Override
    public void initData() {

        String talkTo = getIntent().getStringExtra(Config.TALK_TO);
        dataBinding.etComment.setHint("回复:" + talkTo);

    }

    @Override
    public void setView() {
        dataBinding.etComment.setBackListener(textView -> {
            finish();
        });
        ClickUtil.click(dataBinding.btnSubmit, () -> {
            Intent intent = new Intent();
            intent.putExtra(Config.CONTENT, dataBinding.etComment.getText().toString().trim());
            setResult(Config.SUCCESS, intent);
            finish();
        });
        RxTextView.afterTextChangeEvents(dataBinding.etComment)
                .subscribe(textViewAfterTextChangeEvent -> {
                    if (TextUtils.isEmpty(textViewAfterTextChangeEvent.editable())) {
                        dataBinding.btnSubmit.setEnabled(false);
                    } else {
                        dataBinding.btnSubmit.setEnabled(true);

                    }
                });
    }

    private void setWindow() {
        //窗口对齐屏幕宽度
        Window win = this.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;//设置对话框置顶显示
        win.setAttributes(lp);
    }

    /**
     * EditText获取焦点并显示软键盘
     */
    public static void showSoftInputFromWindow(Activity activity, EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
}
